package com.warm.pump.module.skd.controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.util.StringUtils;
import com.warm.pump.module.skd.bean.SkdJobDtlTreeDto;
import com.warm.pump.module.skd.bean.po.gen.ScheduleJobDtl;
import com.warm.pump.module.skd.service.IScheduleJobDtlService;
import com.warm.pump.module.work.bean.WorkTree;
import com.warm.pump.module.work.service.IWorkService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * skd_job_dtl
 */
@Controller
@RequestMapping("/scheduleJobDtl")
public class ScheduleJobDtlController {
    @Autowired
    private IScheduleJobDtlService scheduleJobDtlService;
    @Autowired
    private IWorkService workService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("scheduleJobDtl:list")
    public OutJson list(Integer page, Integer limit, ScheduleJobDtl scheduleJobDtl) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, scheduleJobDtl);

        map.put("orderBy","exec_num");
        if (StringUtils.isNotEmpty(scheduleJobDtl.getSidx()) && StringUtils.isNotEmpty(scheduleJobDtl.getOrder())) {
            map.put("orderBy", scheduleJobDtl.getSidx() + " " + scheduleJobDtl.getOrder());
        }
        //查询列表数据
        PageBean pageBean = scheduleJobDtlService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }

    /**
     * 所有菜单列表
     */
    @ResponseBody
    @RequestMapping("/treelist")
    public OutJson treelist(String nodeid,Long jobId) {
        Map<String, Object> map = new HashMap<>();

        if(StringUtils.isEmpty(nodeid)){
            map.put("type","workSubject");
        }else {
            map.put("type", "workItem");
            map.put("pid",nodeid);
        }
        map.put("jobId",jobId);
        map.put("orderBy","exec_num");
        List<ScheduleJobDtl> scheduleJobDtls = scheduleJobDtlService.queryList(map);
        List<WorkTree> treeList = new ArrayList<>();
        if(null != scheduleJobDtls&&scheduleJobDtls.size()>0){
            for (ScheduleJobDtl jobDtl:scheduleJobDtls) {
                WorkTree itemTree =new WorkTree();
                itemTree.setType(jobDtl.getType());
                itemTree.setId(jobDtl.getId());
                if("workSubject".equals(jobDtl.getType())){
                    itemTree.setWorkId(jobDtl.getWorkId());
                    itemTree.setParentId("0");
                    itemTree.setIsLeaf(false);
                    itemTree.setLevel(0);
                }else if("workItem".equals(jobDtl.getType())){
                    itemTree.setWorkId(jobDtl.getWorkId());
                    itemTree.setParentId(jobDtl.getPid());
                    itemTree.setIsLeaf(true);
                    itemTree.setLevel(1);
                }
                itemTree.setOrderNum(jobDtl.getExecNum());
                treeList.add(itemTree);
            }

        }
        return OutJson.ok().setResData(treeList);
    }


    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/itemTreelist")
    public OutJson itemTreelist(Long jobId) {
        List<WorkTree> treeList = workService.getWorkTreeByJobIdList(new HashMap(), jobId);
        return OutJson.ok().setResData(treeList);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("scheduleJobDtl:info")
    public OutJson info(@PathVariable("id") String id) {
        ScheduleJobDtl scheduleJobDtl = scheduleJobDtlService.queryObject(id);

        return OutJson.ok().put("scheduleJobDtl", scheduleJobDtl);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("scheduleJobDtl:save")
    public OutJson save(@RequestBody ScheduleJobDtl scheduleJobDtl) {
        scheduleJobDtlService.save(scheduleJobDtl);

        return OutJson.ok();
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/saveItemTree")
    public OutJson saveItemTree(@RequestBody SkdJobDtlTreeDto jobDtlTree) {
        scheduleJobDtlService.saveJobDtl(jobDtlTree.getJobId(),jobDtlTree.getWorkTrees());
        return OutJson.ok();
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/saveJobDtl")
    public OutJson saveJobDtl(String[] ids, String type, Long jobId) {

        if (null != ids && ids.length > 0) {
            for (String id : ids) {
                ScheduleJobDtl jobDtl = new ScheduleJobDtl();
                jobDtl.setJobId(jobId);
                jobDtl.setType(type);
                jobDtl.setCreateTime(new Date());
                jobDtl.setUpdateTime(new Date());
                scheduleJobDtlService.save(jobDtl);
            }
        }
        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("scheduleJobDtl:update")
    public OutJson update(@RequestBody ScheduleJobDtl scheduleJobDtl) {
        scheduleJobDtlService.update(scheduleJobDtl);

        return OutJson.ok();
    }

    /**
     * 更新执行顺序
     */
    @ResponseBody
    @RequestMapping(value="/updateSort", method = RequestMethod.POST)
    public OutJson updateSort(  String [] ids, Integer[] sorts){
        for(int i = 0;i < ids.length;i++){
            String id = ids[i];
            Integer execNum = sorts[i];
            ScheduleJobDtl dtl = new ScheduleJobDtl();
            dtl.setId(id);
            dtl.setExecNum(execNum);
            scheduleJobDtlService.update(dtl);
        }

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("scheduleJobDtl:delete")
    public OutJson delete(@RequestBody String[] ids) {
        scheduleJobDtlService.deleteBatch(ids);

        return OutJson.ok();
    }

}
